/**
 * Created by bestick on 2015/12/11
 * 登录注册权限验证
 */
$(document).ready(function () {

    /* =================================================================== common ===================================================================== */

    //监听form-control
    $('input.form-control').bind({
        "input propertychange": function () {
            $(this).siblings('p').css('display', $(this).val().length < 1 ? 'block' : 'none');
        },
        "blur": function () {
            $(this).removeClass('input-invalid').siblings('p').css('display', 'none');
            if ($(this).val() == '') {
                $(this).addClass('input-invalid').siblings('p').css('display', 'block');
            }
        }
    });

    /**
     * 显示错误信息
     * @param msg
     */
    function showErrorInfo(msg) {
        $("div.error").html(msg).fadeIn(1000, function () {
            $("div.error").delay(1000).fadeOut(500);
        });
        $('.form-control').addClass('input-invalid');
    }

    /* =================================================================== login ===================================================================== */

    //获取cookie，填充表单
    if ($.cookie("remember-login") == "true") {
        $("#remember-login").prop("checked", true);
        $("#login-username").val($.cookie("login-username"));
        $("#login-password").val($.cookie("login-password"));
    }

    /**
     * 保存表单信息到cookie
     */
    function saveLoginInfoToCookie() {
        if ($('#remember-login').prop('checked') == true) {
            var username = $('#login-username').val();
            var password = $('#login-password').val();
            $.cookie('remember-login', 'true', {expires: 7});
            $.cookie('login-username', username, {expires: 7});
            $.cookie('login-password', password, {expires: 7});
        } else {
            $.cookie('remember-login', 'false', {expires: -1});
            $.cookie('login-username', '', {expires: -1});
            $.cookie('login-password', '', {expires: -1});
        }
    }

    /**
     * 检查登录表单
     * @returns {boolean}
     */
    function checkLoginForm() {
        var username = $('#login-username').val();
        var password = $('#login-password').val();
        if (username == null || username == '') {
            $('#login-username').next().css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (password == null || password == '') {
            $('#login-password').next().css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        $('#login-username').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#login-password').next().css('display', 'none').prev().removeClass('input-invalid');
        return true;
    }

    //点击登录按钮
    $('#login').click(function () {
        var pwdHash = hex_sha1($('#login-password').val());
        if (!checkLoginForm()) {
        }
        else {
            $.ajax({
                type: 'GET',
                data: {
                    userName: $('#login-username').val(),
                    passWord: pwdHash
                },
                url: "/api/client/user/login",
                success: function (result) {
                    if (result.status == 1) {
                        saveLoginInfoToCookie();
                        location.assign(document.referrer);
                        alert('欢迎： ' + $('#login-username').val());
                    } else {
                        showErrorInfo(result.msg);
                    }
                },
                error: function () {
                    alert('error');
                }
            })
        }
    });

    /* =================================================================== signUp ===================================================================== */

    /**
     * 检查注册表单
     * @returns {boolean}
     */
    function checkSignForm() {
        var userphone = $("#sign-userphone").val();
        var username = $('#sign-username').val();
        var password = $('#sign-password').val();
        var confirm = $('#sign-confirm').val();
        var picValidate = $("#sign-picValidate").val();
        var mesValidate = $("#sign-mesValidate").val();
        var checkbox = $('#agree-sign');

        if (userphone == null || userphone == '') {
            $('#sign-userphone').next().css('display', 'block').prev().addClass('input-invalid');
            return false;
        }

        if (username == null || username == '') {
            $('#sign-username').next().css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (password == null || password == '') {
            $('#sign-password').next().css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (confirm == null || confirm == '') {
            $('#sign-confirm').next().css('display', 'block').prev().addClass('input-invalid');
            return false;
        }

        if (picValidate == null || picValidate == '') {
            $('#sign-picValidate').find("p").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }

        if (mesValidate == null || mesValidate == '') {
            $('#sign-mesValidate').find("p").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }

        if (!checkbox.prop("checked")) {
            $('#agree-sign').next().next().css('display', 'block');
            return false;
        }
        $('#sign-username').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#sign-password').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#sign-confirm').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#agree-sign').next().next().css('display', 'none');
        return true;
    }

    //同意聚财邦申明
    $('#agree-sign').change(function () {
        $(this).next().next().css('display', $(this).prop('checked') ? 'none' : 'block');
    });

    //点击注册按钮
    $('#sign').click(function () {
        var pwdHash = hex_sha1($('#sign-password').val());
        if (!checkSignForm()) {
        }
        else if ($('#sign-confirm').val() != $('#sign-password').val()) {
            showErrorInfo("两次输入密码不一致，请重试");
            $('#sign-username').removeClass('input-invalid');
        }
        else {
            $.ajax({
                type: 'GET',
                data: {
                    userName: $('#sign-username').val(),
                    //TODO 昵称暂定
                    nickName: $('#sign-username').val(),
                    passWord: pwdHash
                },
                url: "/api/client/user/register",
                success: function (result) {
                    if (result.status == 1) {
                        location.assign("/index");
                        alert('欢迎： ' + $('#sign-username').val());
                    } else {
                        showErrorInfo(result.msg);
                        $('.form-control').not($('#sign-username')).removeClass('input-invalid');
                    }
                },
                error: function () {
                    alert('error');
                }
            })
        }
    });

    /* =================================================================== openAccount ===================================================================== */

    //点击协议弹出modal
    $('.openAcc-protocol').click(function () {
        $('#protocolModal').modal('show');
    });
});